package com.tau.egonetwork.gui.core;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

public class EgoNetworkMessageBox extends Dialog {
	
	private String _message;
	private int _buttonsMask;
	private int _icon;
	
	private int _returnCode = 0;
	
	public EgoNetworkMessageBox(Shell parent, int icon, int buttons) {
		super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		_icon = icon;
		_buttonsMask = buttons;
	}

	public void setMessage(String message) {
		_message = message;
	}
	
	public int open() {
		Shell shell = new Shell(getParent(), getStyle());
		shell.setText(getText());
				
		createDialogContent(shell);
		
		shell.pack();
		
		//set shell at the center of display:
	    Monitor primary = getParent().getMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    shell.setLocation(x, y);
		
		shell.open();
		
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		return _returnCode;
	}
	
	private void createDialogContent(final Shell shell) {
		shell.setLayout(new GridLayout(1, true));
		
		Composite messageComposite = new Composite(shell, SWT.NONE);
		GridLayout messageCompositeLayout = new GridLayout(2, false);
		messageCompositeLayout.marginTop = 5;
		messageCompositeLayout.marginBottom = 5;
		messageCompositeLayout.marginLeft = 5;
		messageCompositeLayout.marginRight = 5;
		messageCompositeLayout.horizontalSpacing = 15;
		messageComposite.setLayout(messageCompositeLayout);
		GridData messageCompositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		messageCompositeGridData.widthHint = 550;
		messageComposite.setLayoutData(messageCompositeGridData);
		
		//display an icon:
		Label imgLabel = new Label(messageComposite, SWT.NONE);
		imgLabel.setImage(shell.getDisplay().getSystemImage(_icon));
		imgLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, true));
		
		//show the message:
		Label msgLabel = new Label(messageComposite, SWT.WRAP);
		msgLabel.setText(_message);
	    msgLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true));
	    
	    Composite buttonsComposite = new Composite(shell, SWT.NONE);
		GridLayout buttonsCompositeLayout = new GridLayout(4, false);
		buttonsCompositeLayout.marginTop = 15;
		buttonsCompositeLayout.marginBottom = 5;
		buttonsCompositeLayout.marginLeft = 0;
		buttonsCompositeLayout.marginRight = 0;
		buttonsCompositeLayout.horizontalSpacing = 10;
		buttonsComposite.setLayout(buttonsCompositeLayout);
		buttonsComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
		
		int possibleButtons[] = { SWT.OK, SWT.YES, SWT.NO, SWT.RETRY, SWT.CANCEL, SWT.ABORT };
		String possibleButtonsText[] = { "OK", "Yes", "No", "Retry", "Cancel", "Abort" };
		
		GridData buttonGridData = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		buttonGridData.widthHint = 90;
		
		for (int i = 0; i < possibleButtons.length; i++) {
			final int possibility = possibleButtons[i];
			if ((_buttonsMask & possibility) != 0) {
				Button b = new Button(buttonsComposite, SWT.PUSH);
				b.setText(possibleButtonsText[i]);
				b.setLayoutData(buttonGridData);
				b.addListener(SWT.Selection, new Listener() {
					
					@Override
					public void handleEvent(Event event) {
						_returnCode = possibility;
						shell.dispose();
					}
				});
			}
		}

	}
}
